﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExtAspNet;
using FJASPX.Service;
using FJASPX.Framwork.UI;
using log4net;
using FJASPX.Framwork.Helper;
using FJASPX.Framwork.Constant;

namespace PlugNT.Cms.Web.Member.News
{
    public partial class Edit : EditPageBase<FJASPX.Domain.Article>
    {
        public IArticleService iArticleService { get; set; }

        #region Properties属性

        public override string PowerCode
        {
            get { return "51f0cdc42e0a4cdc8290f2e87db79d34"; }
        }

        #endregion

        #region Methods方法

        protected override bool FirstCallRead()
        {
            //初始化控件
            this.btn_BReturn = this.btn_Return;
            base.FirstCallRead();
            this.dp_ReleaseTimeD.SetValue(DateTime.Now);
            //判断更新还是添加
            if (this.IsUpdate)
            {
                int id = Convert.ToInt32(StringHelper.GetQueryString(Strings.ID));
                this.Model = iArticleService.Get(id);
                if (!this.Model.Id.IsNull())
                {
                    this.txt_NameD.SetValue(this.Model.Title);
                    this.cb_StatusD.SetValue(this.Model.State);
                    this.cb_IsTopD.SetValue(this.Model.State);
                    this.dp_ReleaseTimeD.SetValue(this.Model.TimeStamp);
                    this.txt_ContentD.Text = this.Model.Context;
                    this.txt_SourceD.SetValue(this.Model.From);
                }
            }
            return true;
        }

        #endregion

        #region Events事件

        protected void btn_Save_Click(object sender, EventArgs e)
        {
            if (this.IsUpdate)
            {
                int id = Convert.ToInt32(StringHelper.GetQueryString(Strings.ID));
                this.Model = iArticleService.Get(id);
            }
            else
            {
                this.Model = new FJASPX.Domain.Article();
                this.Model.State = 1;
                this.Model.Creator = this.CurrentUser.UserName;
                this.Model.UserId = 0;
                this.Model.UserName = this.CurrentUser.UserName;
            }

            this.Model.From = this.txt_SourceD.GetValue();
            this.Model.Title = this.txt_NameD.GetValue();
            this.Model.Context = this.txt_ContentD.Text;

            try
            {
                if (this.Model.Id != null)
                    iArticleService.Update(this.Model);
                else
                    iArticleService.Save(this.Model);

                //这里加清缓存操作
                this.Success();
                return;
            }
            catch (Exception ex)
            {
                Log.WriteLog(Message.ERR, ex);
            }
            this.Fail();
        }

        #endregion
    }
}